package mashuaiproject.mashuaiproject.controller;


import mashuaiproject.mashuaiproject.mapper.AttributeMappper;
import mashuaiproject.mashuaiproject.model.GuanZhu;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//这个类是对用户属性进行一系列的操作
@Controller
public class AttributeContrller {
    @Autowired
    private AttributeMappper attributeMappper;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
    //关注博主
    @ResponseBody
    @GetMapping("/NewGuanZhu")
    public Map<String ,Object> NewGuanZhu(@RequestParam("boZhuId")Integer boZhuId,
                                          @RequestParam("judegGuanzhu") String judegGuanzhu,
                                          HttpServletRequest request){
        Map<String ,Object> map=new HashMap<>();
        Users users= (Users) request.getSession().getAttribute("user");
        if(judegGuanzhu.equals("未关注")){
            GuanZhu guanZhu=new GuanZhu();
            guanZhu.setUser_id(users.getId());
            guanZhu.setBozhu_id(boZhuId);
            guanZhu.setGmt_create(System.currentTimeMillis());
            //插入
            int insert= attributeMappper.insertNewGuanZhu(guanZhu);
            //给博主关注量+1
            int updateAdd=attributeMappper.updateAddBoZhuFensi(boZhuId);
            jMontiorSerivce.checkHasGuaZhu(users.getId(),boZhuId,"add");
            jMontiorSerivce.Upleave(users.getId());
            //插入关注的动态

            Integer insert2 =jMontiorSerivce.insertDongtai(users.getId(),3,boZhuId);//用户id，动态类型，不集体类型

            map.put("mes","已关注");
        }
        if(judegGuanzhu.equals("已关注")){
            //删除这个记录
            int del=attributeMappper.delGuanzhu(users.getId(),boZhuId);
            int updatesReds=attributeMappper.updateARedsBoZhuFensi(boZhuId);
            jMontiorSerivce.checkHasGuaZhu(users.getId(),boZhuId,"reds");
            jMontiorSerivce.Upleave(users.getId());
            Integer insert =jMontiorSerivce.insertDongtai(users.getId(),4,boZhuId);//用户id，动态类型，不集体类型
            map.put("mes","未关注");
        }
        //找出当前数量
        Integer fensiCount=attributeMappper.selectFenSiCount(boZhuId);
        System.err.println("当前粉丝："+fensiCount);
        map.put("fensiCount",fensiCount);
        return  map;
    }
}
